package com.jfreer.mybatis;

import org.mybatis.generator.api.MyBatisGenerator;
import org.mybatis.generator.config.*;
import org.mybatis.generator.config.xml.ConfigurationParser;
import org.mybatis.generator.exception.InvalidConfigurationException;
import org.mybatis.generator.exception.XMLParserException;
import org.mybatis.generator.internal.DefaultShellCallback;

import java.io.IOException;
import java.io.InputStream;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * Hello world!
 */
public class CodeGenerator {
    /**
     * 生成代码的根包名
     */
    public final static String GENERATOR_PKG = "gen";
    /**
     * 源码目录位置.生成代码放置位置
     */
    public final static String SOURCE_PATH = "/Volumes/apple/sources/self/mybatis-code-generator/how-to-use/target";
    /**
     * Mapper接口的子包名
     */
    public static final String DATABASE_MAPPER = ".mapper";
    /**
     * Xml配置文件的子包名
     */
    public static final String DATABASE_XML = ".xml";
    /**
     * Model实体的子包名
     */
    public static final String DATABASE_MODEL = ".model";
    /**
     * 要读取的配置文件在Classpath中的位置
     */
    public static final String GENERATOR_CONFIG_SQLSERVER_XML = "E:\\workspace\\mybatis-code-generator\\how-to-use\\conf\\mybatis-generator\\generatorConfig.xml";

    public static void main(String[] args) {
        try {
            List<String> warnings = new ArrayList<String>();
            boolean overwrite = true;
            ConfigurationParser cp = new ConfigurationParser(warnings);
            InputStream resourceAsStream = CodeGenerator.class.getResourceAsStream(GENERATOR_CONFIG_SQLSERVER_XML);
            Configuration config = cp.parseConfiguration(resourceAsStream);
            setPackageAndPath(config);
            DefaultShellCallback callback = new DefaultShellCallback(overwrite);
            MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config, callback, warnings);
            myBatisGenerator.generate(null);
            if (warnings.size() > 0) {
                System.out.println("以下为生成过程产生的警告信息：");
                for (String s : warnings) {
                    System.out.println(s);
                }
            }
            System.out.println("结束........");
        } catch (IOException e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        } catch (XMLParserException e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        } catch (InvalidConfigurationException e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        } catch (SQLException e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        } catch (InterruptedException e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        }
    }

    public static void setPackageAndPath(Configuration packageAndPath) {
        for (Context ctx : packageAndPath.getContexts()) {
            JavaClientGeneratorConfiguration javaClientGeneratorConfiguration = ctx.getJavaClientGeneratorConfiguration();
            javaClientGeneratorConfiguration.setTargetPackage(GENERATOR_PKG + DATABASE_MAPPER);
            javaClientGeneratorConfiguration.setTargetProject(SOURCE_PATH);

            SqlMapGeneratorConfiguration sqlMapGeneratorConfiguration = ctx.getSqlMapGeneratorConfiguration();
            sqlMapGeneratorConfiguration.setTargetProject(SOURCE_PATH);
            sqlMapGeneratorConfiguration.setTargetPackage(GENERATOR_PKG + DATABASE_XML);

            JavaModelGeneratorConfiguration javaModelGeneratorConfiguration = ctx.getJavaModelGeneratorConfiguration();
            javaModelGeneratorConfiguration.setTargetProject(SOURCE_PATH);
            javaModelGeneratorConfiguration.setTargetPackage(GENERATOR_PKG + DATABASE_MODEL);
        }
    }
}